﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Aulac.Rsc.Bss.Tags;
using Aulac.Global.Xml;
using System.Xml;
using Aulac.Srv.Db.Connect;
using Aulac.Srv.Db.Common;
using Aulac.Global.Common;
using Aulac.Rsc.Bss;
using Aulac.Srv.Data.XmlQuery;

namespace Aulac.Srv.Db.Data.Ems.Items.RptLists
{
    public partial class TabDepr : Aulac.Srv.Db.Data.Ems.Document.DbEmsDataDocumentMangager
    {
        public TabDepr(IDbAulacConnect p_Db)
            : base(p_Db)
        {

        }

        /// <summary>
        /// Add thông tin một bảng Khấu hao.
        /// </summary>
        public XmlDataDocument AddTabDepr(XmlDataDocument xmlData)
        {
            try
            {
                XmlElement eTmpH = XmlProcess.GetVchHeaderElm(xmlData);
                XmlElement eTmpL = XmlProcess.GetVchLinesElm(xmlData);

                ExecQueryAddTabHeaderDepr(eTmpH);
                string sTabPrkID = DbDataProvider.SelectMaxID(TableNames.TableHeaderDepreciation, tagTab.TabHeaderDepr.TabDeprPrkID);
                ExecQueryAddTabLineDepr(XmlProcess.GetListVchLineItem(xmlData), sTabPrkID);
                return xmlData; 
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Cập nhật thông tin một bảng Khấu hao.
        /// </summary>
        public XmlDataDocument UpdateTabDepr(XmlDataDocument xmlQuery)
        {
            try
            {
                XmlElement eTmpH = XmlProcess.GetVchHeaderElm(xmlQuery);
                XmlElement eTmpL = XmlProcess.GetVchLinesElm(xmlQuery);
                string mTabDeprPrkID = XmlProcess.GetValueVchHeader(xmlQuery, tagTab.TabHeaderDepr.TabDeprPrkID);
                ExecQueryUpdateHeaderTabDepr(mTabDeprPrkID, eTmpH);
                DelTabDeprLine(mTabDeprPrkID);
                ExecQueryAddTabLineDepr(XmlProcess.GetListVchLineItem(xmlQuery), mTabDeprPrkID);
                return xmlQuery;
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Xóa một bảng Khấu hao.
        /// </summary>
        public XmlDataDocument DelTabDepr(XmlDataDocument xmlData)
        {
            try
            {
                string sTabDeprPrkID = XmlProcess.GetValueParam(xmlData, tagTab.TabHeaderDepr.TabDeprPrkID);
                return ExecQueryDelTabDepr(sTabDeprPrkID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
        }

        /// <summary>
        /// Get thông tin chi tiết của một phiếu.
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public XmlDataDocument GetDetailTabDepr(XmlDataDocument xmlData)
        {            
            try
            {
                string sTabDeprPrkID = XmlProcess.GetValueParam(xmlData, tagTab.TabHeaderDepr.TabDeprPrkID);
                return ExecQueryGetDetailTabDepr(sTabDeprPrkID);
            }
            catch (Exception Ex)
            {
                throw Ex;
            }
          
        }

        /// <summary>
        /// Get danh sach các phiếu.
        /// </summary>
        /// <param name="xmlData"></param>
        /// <returns></returns>
        public XmlDataDocument GetListTabDepr(XmlDataDocument xmlData)
         {
             string sTabDateFrom = XmlProcess.GetValueParam(xmlData, tagGeneral.TabFromDate);
             string sTabDateTo = XmlProcess.GetValueParam(xmlData, tagGeneral.TabToDate);
            XmlDataDocument xmlTest = ExecQueryGetListTabDepr(sTabDateFrom, sTabDateTo);
            return xmlTest;
         }

        public XmlDataDocument GetNewVchNo(XmlDataDocument xmlData)
        {
            string sVchTypeID = XmlProcess.GetValueParam(xmlData, tagVch.VchBuyHeaderItem.VchTypeID);
            XmlDataDocument xmlOutDoc = CreateXmlDocument("", "");
            string sVchName = "";
            string sVchNo = GetNewVchNo(TableNames.VchHeaderBuyItem, tagVch.VchBuyHeaderItem.VchNo, sVchTypeID, ref sVchName);
            XmlProcess.SetAtrbVchHeader(xmlOutDoc, tagVch.VchBuyHeaderItem.VchNo, sVchNo);
            XmlProcess.SetAtrbVchHeader(xmlOutDoc, tagVch.VchBuyHeaderItem.VchName, sVchName);
            return xmlOutDoc;
        }

        #region Private Methods
        //private bool DelDbTableVchBuy(string pTableName, string pTagKey, string pValueKey)
        //{
        //    try
        //    {
        //        string sqlQuery = SqlHelper.CreateSqlDelete(pTableName, pTagKey);
        //        return DbDataProvider.ExecuteNonQuery(ref this.mDbCmd, sqlQuery, pValueKey) > 0 ? true : false;

        //    }
        //    catch (Exception Ex)
        //    {
        //        throw Ex;
        //    }
        //}
        //private bool AddDbTableVchBuy(string pTableName, string[] ArrL, params object[] arrValues)
        //{
        //    try
        //    {
        //        string sqlQuery = SqlHelper.CreateSqlInsert(pTableName, ArrL);
        //        DbDataProvider.SelectMaxID(pTableName, tagVch.VchBuyHeaderItem.VchBuyItemPrkID);
        //        return DbDataProvider.ExecuteNonQuery(ref this.mDbCmd, sqlQuery, arrValues) > 0 ? true : false;
        //    }
        //    catch (Exception Ex)
        //    {
        //        throw Ex;
        //    }
        //}
        #endregion
    }
}
